package com.qst.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.mapper.EquipmentMapper;
import com.qst.pojo.Employee;
import com.qst.pojo.Equipment;
import com.qst.pojo.Result;
import com.qst.pojo.User;
import com.qst.query.EmployeeQuery;
import com.qst.query.EquipmentQuery;
import com.qst.query.MemberQuery;
import com.qst.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassNmae: EmployeeController
 * @Author: 邓嘉顺
 * @Description:
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;
    @RequestMapping("/ToEmployee")
    public String ToEmployee(){
        return "employeeInfo";
    }


    @RequestMapping("/ToEmployeeInfo")
    @ResponseBody
    public Result ToEmployeeInfo(Model model, @ModelAttribute EmployeeQuery employeeQuery) throws JsonProcessingException {
        PageHelper.startPage(employeeQuery.getPage(),employeeQuery.getLimit());
        List<Employee> equipmentList=   employeeService.queryAllEmployee( employeeQuery.getEmployeeAccount(),employeeQuery.getEmployeeName());
        PageInfo<Employee> pageInfo = new PageInfo<>(equipmentList);
        List<Employee> data = pageInfo.getList();
        int count =   employeeService.getAllCount();
        return  new Result(0,"",data,count);
    }

    @RequestMapping("/EditEmployee")
    @ResponseBody
    public Result EditMember(@RequestParam("id") Integer id, @RequestParam("field") String field, @RequestParam("value") String value){
//        转换，将userAge转换为user_age
        String columnName =   field.replaceAll("[A-Z]", "_$0").toLowerCase();    // 数据库中对应的列名
        Result result=employeeService.EditEmployee(id,columnName,value);
        return result;
    }


    @RequestMapping("/addEmployee")
    @ResponseBody
    public Result addEmployee(Employee employee){

        Random random = new Random();
        String account1 = "1010";
//        将五个0到9的随机数字拼接成一个字符串,再拼接2023为会员账号account
        for (int i = 0; i < 5; i++) {
            account1 += random.nextInt(10);
        }
        String account =account1;
        //初始密码
        String password = "123456";

//获取当前日期，并转换格式
//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String nowDay = simpleDateFormat.format(date);

        employee.setEmployeeAccount(account);
//        user.setCardTime(nowDay);
//        user.setUserCard(account);
//        user.setUserPwd(password);
//        user.setRoleId(1);

        Result result = employeeService.addEmployee(employee);
        return result;
    }

    @RequestMapping("/delEmployee")
    @ResponseBody
    public Result delMember(@RequestParam("ids[]") Integer[] ids){
        Result result = employeeService.delBatch(ids);
        return result;
    }
}
